import { basicBracketEnum, basicOperatorEnum, operatorPriorityEnum, quoteBracketEnum, quoteOperatorEnum } from "@base/enum";
import { TipsKindEnum } from "../component/enum";
import { BracketLeft, BracketRight } from "@base/Bracket";
import { Operator } from "@base/Operator";
import { add, divide, minus, multiply, surplus } from "@base/compute";
import { eventBus } from "./initEventBus";
import { isBracket, isOperator } from "../handleBase/isInstanceOf";
const store = eventBus.store;
const { Tips, TypingRegister, BracketLIndexs, BracketRIndexs } = eventBus.store;

export const BracketL = new BracketLeft({
    basic: basicBracketEnum.left,
    quote: quoteBracketEnum.left
});
/**
 * 点击左括号，判断此次左括号，能否替换寄存器上一次输入的内容，并将上一次寄存器的内容整理进中缀表达式
 * @returns void
 */
BracketL.action = function () {
    const prevTyped = TypingRegister.getPacket();
    // 如果寄存器为空（第一位），直接放入寄存器
    if (prevTyped === undefined) {
        TypingRegister.tryPush(this);
        return;
    }

    if (isBracket(prevTyped, 'right') || typeof (prevTyped) === 'number') {
        Tips.show(TipsKindEnum.notice, '右括号、数字后面不能直接加左括号');
        return;
    }

    store.infixArr.push(prevTyped);
    isBracket(prevTyped, 'left') && BracketLIndexs.pushNewIndex(store.infixArr.length - 1);
    TypingRegister.tryPush(this);
}

export const BracketR = new BracketRight({
    basic: basicBracketEnum.right,
    quote: quoteBracketEnum.right
});
/**
 * 点击右括号，判断此次右括号，能否替换寄存器上一次输入的内容，并将上一次寄存器的内容整理进中缀表达式
 * @returns void
 */
BracketR.action = function () {
    if (BracketRIndexs.countSize() === BracketLIndexs.countSize()) {
        Tips.show(TipsKindEnum.notice, '禁止输入多余右括号');
        return;
    }

    const prevTyped = TypingRegister.getPacket();

    if (prevTyped === undefined) {
        Tips.show(TipsKindEnum.notice, '右括号不能放在开头');
        return;
    }
    if (isOperator(prevTyped)) {
        Tips.show(TipsKindEnum.notice, '运算符后面不能加右括号');
        return;
    }
    if (isBracket(prevTyped, 'left')) {
        Tips.show(TipsKindEnum.notice, '括号里面要有运算');
        return;
    }
    if (isBracket(prevTyped, 'right')) {
        if (BracketRIndexs.countSize() - BracketLIndexs.countSize() === -1) {
            Tips.show(TipsKindEnum.notice, '右括号够了，不能再输入');
            return;
        }
        // 提前记录索引
        BracketRIndexs.pushNewIndex(store.infixArr.length);
    }
    store.infixArr.push(prevTyped);
    TypingRegister.tryPush(this);
}

export const Add = new Operator({
    basic: basicOperatorEnum.add,
    priority: operatorPriorityEnum.secondary,
    quote: quoteOperatorEnum.add
}, add);

export const Minus = new Operator({
    basic: basicOperatorEnum.minus,
    priority: operatorPriorityEnum.secondary,
    quote: quoteOperatorEnum.minus
}, minus);

export const Multiply = new Operator({
    basic: basicOperatorEnum.multiply,
    priority: operatorPriorityEnum.headmost,
    quote: quoteOperatorEnum.multiply
}, multiply);

export const Divide = new Operator({
    basic: basicOperatorEnum.divide,
    priority: operatorPriorityEnum.headmost,
    quote: quoteOperatorEnum.divide
}, (a, b) => {
    if (b === 0) {
        Tips.show(TipsKindEnum.cartion, '不能除以零，除以零部分不会被计算');
        return 0;
    }
    return divide(a, b)
});

export const Surplus = new Operator({
    basic: basicOperatorEnum.surplus,
    priority: operatorPriorityEnum.headmost,
    quote: quoteOperatorEnum.surplus
}, (a, b) => {
    if (b === 0) {
        Tips.show(TipsKindEnum.cartion, '不能除以零，求余部分的结果不会被计算');
        return 0;
    }
    return surplus(a, b)
});


